package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffContractListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.manager.UecOutStaffContractManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffContract;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:外包人员合同
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月19日
 */
@Controller
@Scope("prototype")
public class UecOutStaffContractListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = -5811398149406451671L;

    /**
     * 外包人员基本信息功能 查询出的实体对象
     */
    private UecOutStaffInfo uecOutStaffInfo;

    /**
     * 对应页面上的bean
     */
    private UecOutStaffContractListboxBean bean = new UecOutStaffContractListboxBean();

    /**
     * 外包人员工作记录实体
     */
    private UecOutStaffContract uecOutStaffContract;

    /**
     * 外包人员工作记录查询实体
     */
    private UecOutStaffContract qryUecOutStaffContract;

    private UecOutStaffContractManager uecOutStaffContractManager = (UecOutStaffContractManager) ApplicationContextUtil
            .getBean("uecOutStaffContractManager");

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecOutStaffContractListboxExt() {
        Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_CONTRACT_EXT_LISTBOX_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_CONTRACT_QUERY, this,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_CONTRACT_QUERY_RESPONSE);
        this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_CONTRACT, this,
                UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_CONTRACT_RESPONSE);
    }

    /**
     * 初始化
     */
    public void onCreate() {
        this.setButtonValid(false, false, false, false);
    }

    /**
     * 选择查询记录
     * 
     * @throws Exception
     */
    public void onSelectUecOutStaffContractList() throws Exception {
        if (this.bean.getUecOutStaffContractListbox().getSelectedCount() > 0) {
            uecOutStaffContract = (UecOutStaffContract) bean.getUecOutStaffContractListbox().getSelectedItem()
                    .getValue();
            Events.postEvent("onContract", this, uecOutStaffContract);
        }
    }

    /**
     * 人员基本信息功能，抛出事件， 事件监听
     * 
     * @param event
     */
    public void onUecOutStaffContractQueryResponse(ForwardEvent event) {
        uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
        if (null != uecOutStaffInfo) {
            //设置操作按钮，非盟市主管不可操作按钮
            if(PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, false, false, false);
            }else {
                this.setButtonValid(true, true, false, false);
            }
            qryUecOutStaffContract = new UecOutStaffContract();
            if (!StrUtil.isEmpty(uecOutStaffInfo.getPkPsnMsg())) {
                qryUecOutStaffContract.setPkPsnMsg(uecOutStaffInfo.getPkPsnMsg());
            }
            qryUecOutStaffContract.setOutStaffId(uecOutStaffInfo.getOutStaffId());
            this.onQueryUecOutStaffContractQuery();
        }else {
            clearVariable();
            uecOutStaffInfo = null;
            ListboxUtils.clearListbox(this.bean.getUecOutStaffContractListbox());
            this.bean.getUecOutStaffContractListboxPaging().setTotalSize(1);
        }
    }

    /**
     * 查询按钮
     */
    public void onQueryUecOutStaffContractQuery() {
        clearVariable();
        UecOutStaffContract objUecOutStaffContract = UecOutStaffContract.newInstance();
        PubUtil.fillPoFromBean(bean, objUecOutStaffContract);
        qryUecOutStaffContract = objUecOutStaffContract;
        this.bean.getUecOutStaffContractListboxPaging().setActivePage(0);
        this.onQueryUecOutStaffContractList();
    }

    /**
     * 重置按钮
     */
    public void onResetUecOutStaffContract() {
        ListboxUtils.clearListbox(this.bean.getUecOutStaffContractListbox());
        this.bean.getUecOutStaffContractListboxPaging().setTotalSize(1);
    }

    /**
     * 清除
     * 
     * @param event
     */
    public void onCleanUecOutContractResponse(ForwardEvent event) {
        onCleanUecOutStaffContractResponse();
    }

    public void onCleanUecOutStaffContractResponse() {
        clearVariable();
        uecOutStaffInfo = null;
        ListboxUtils.clearListbox(this.bean.getUecOutStaffContractListbox());
        this.bean.getUecOutStaffContractListboxPaging().setTotalSize(1);
        this.setButtonValid(false, false, false, false);
    }

    /**
     * 设置按钮
     * 
     * @param canAdd
     * @param candel
     */
    private void setButtonValid(boolean canAdd, boolean canEdit, boolean candel, boolean canView) {
        this.bean.getAddButton().setDisabled(!canAdd);
        this.bean.getEditButton().setDisabled(!canEdit);
        this.bean.getDelButton().setDisabled(!candel);
        this.bean.getViewButton().setDisabled(!canView);
    }

    /**
     * 新增按钮
     * 
     * @throws Exception
     *             @throws
     */
    public void onAddUecOutStaffContract() throws Exception {
        openUecOutStaffContractEditWin("add");
        this.onQueryUecOutStaffContractQuery();
    }

    /**
     * 修改按钮
     * 
     * @throws Exception
     *             @throws
     */
    public void onEditUecOutStaffContract() throws Exception {
        if (this.uecOutStaffContract != null) {
            openUecOutStaffContractEditWin("edit");
        } else {
            ZkUtil.showError("请选择你要修改的记录", "提示信息");
        }
    }

    /**
     * 查看按钮
     * 
     * @throws Exception
     *             @throws
     */
    public void onViewUecOutStaffContract() throws Exception {
        if (this.uecOutStaffContract != null) {
            openUecOutStaffContractEditWin("view");
        } else {
            ZkUtil.showError("请选择你要查看的记录", "提示信息");
        }
    }

    /**
     * openUacStaffInfoWin
     * 
     * @param opType
     */
    private void openUecOutStaffContractEditWin(String opType) {
        if (null != uecOutStaffInfo) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", opType);
            map.put("uecOutStaffInfo", uecOutStaffInfo);
            map.put("uecOutStaffContract", uecOutStaffContract);
            if ("edit".equals(opType)) {
                map.put("uecOutStaffContractId", uecOutStaffContract.getId());
            }
            Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_CONTRACT_EDIT_ZUL,
                    this, map);
            try {
                window.doModal();
                window.addEventListener("onOK", new EventListener() {

                    @Override
                    public void onEvent(Event event) throws Exception {
                        if (event.getData() != null) {
                            // 新增或修改完成后，列表从数据库更新
                            uecOutStaffContract = (UecOutStaffContract) event.getData();
                            onQueryUecOutStaffContractList();
                        }
                    }
                });
            } catch (SuspendNotAllowedException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除按钮
     */
    public void onDelUecOutStaffContract() {
        if (this.uecOutStaffContract != null) {
            ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {

                public void onEvent(Event event) throws Exception {
                    Integer result = (Integer) event.getData();
                    if (result == Messagebox.OK) {
                        uecOutStaffContract.removeOnly();
                        PubUtil.reDisplayListbox(bean.getUecOutStaffContractListbox(), uecOutStaffContract, "del");
                        uecOutStaffContract = null;
                        setButtonValid(true, false, false, false);
                    }
                }
            });
        } else {
            ZkUtil.showError("请选择你要删除的记录", "提示信息");
            return;
        }
    }

    /**
     * 翻页
     */
    public void onUecOutStaffContractListboxPaging() {
        this.onQueryUecOutStaffContractList();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecOutStaffContract = null;
        qryUecOutStaffContract = null;
    }

    /**
     * 查询
     */
    public void onQueryUecOutStaffContractList() {
        if (null != uecOutStaffInfo) {
            PageInfo pageInfo = uecOutStaffContractManager.queryPageInfoByUecOutStaffContract(uecOutStaffInfo,
                    this.bean.getUecOutStaffContractListboxPaging().getActivePage() + 1,
                    this.bean.getUecOutStaffContractListboxPaging().getPageSize());
            ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
            this.bean.getUecOutStaffContractListbox().setModel(list);
            this.bean.getUecOutStaffContractListboxPaging().setTotalSize(pageInfo.getTotalCount());
        }
    }
}
